﻿//=============================================================================
// LazyParser.NET - C# Expression Parser for .NET 2.0 
//
// Copyright (c) 2008 Philippe Leybaert
//
// Permission is hereby granted, free of charge, to any person obtaining a copy 
// of this software and associated documentation files (the "Software"), to deal 
// in the Software without restriction, including without limitation the rights 
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
// copies of the Software, and to permit persons to whom the Software is 
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in 
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//=============================================================================

using System;
using System.Collections.Generic;

namespace Activa.LazyParser
{
    public abstract class Expression : IExpression
    {
        public abstract ValueExpression Evaluate(IParserContext context);

        protected static ValueExpression[] EvaluateExpressionArray(Expression[] expressions, IParserContext context)
        {
            return Array.ConvertAll<Expression, ValueExpression>(expressions, delegate(Expression expr) { return expr.Evaluate(context); });
        }

    	object IExpression.EvaluateToObject(IParserContext context)
    	{
    		return Evaluate(context).Value;
    	}

    	IValueWithType IExpression.Evaluate(IParserContext context)
    	{
    		ValueExpression value = Evaluate(context);

            return new ValueExpression(value.Value,value.Type);
    	}

    	public T Evaluate<T>(IParserContext context)
    	{
    		return (T) Evaluate(context).Value;
    	}
    }

    public static class Exp
    {
        public static AddExpression Add(Expression left, Expression right) { return new AddExpression(left, right); }
        public static SubtractExpression Subtract(Expression left, Expression right) { return new SubtractExpression(left, right); }
        public static MultiplyExpression Multiply(Expression left, Expression right) { return new MultiplyExpression(left, right); }
        public static DivideExpression Divide(Expression left, Expression right) { return new DivideExpression(left, right); }
        public static ValueExpression<T> Value<T>(T value) { return new ValueExpression<T>(value); }
        public static ValueExpression Value(object value, Type type) { return new ValueExpression(value,type); }
        public static BinaryArithmicExpression Op(string op, Expression left, Expression right) { return new BinaryArithmicExpression(op, left, right); }
        public static AndAlsoExpression AndAlso(Expression left, Expression right) { return new AndAlsoExpression(left, right); }
        public static OrElseExpression OrElse(Expression left, Expression right) { return new OrElseExpression(left, right); }
    }
}
